package com.example.administrator.smartdevice.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.smartdevice.R;
import com.example.administrator.smartdevice.activity.DeviceActivity;
import com.example.administrator.smartdevice.activity.MainActivity2;
import com.example.administrator.smartdevice.activity.PermissionManageActivity;
import com.example.administrator.smartdevice.activity.TestGenerateActivity;
import com.example.administrator.smartdevice.adapter.HomePageFunctionAdapter;
import com.example.administrator.smartdevice.base.BaseFragment;
import com.example.administrator.smartdevice.model.FunctionModel;
import com.example.administrator.smartdevice.utils.DisplayHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/4/23.
 */

public class HomePageFragment extends BaseFragment {
    @BindView(R.id.fragment_home_page_rec_function)
    protected RecyclerView fragment_home_page_rec_function;
    private int[] functionIconList = {R.mipmap.ic_launcher, R.mipmap.ic_launcher};
    private String[] functionTitleList = {"设备管理", "权限管理"};

    public static HomePageFragment newInstance() {
        return new HomePageFragment();
    }

    @Override
    protected void initData(View view, Bundle savedInstanceState) {
        setStatusBar();
        initFunction();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_page;
    }

    @Override
    protected void onFailure(int requestCode, Object object) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object) {

    }

    public void initFunction() {
        List<FunctionModel> list = new ArrayList<>();
        for (int i = 0; i < functionIconList.length; i++) {
            FunctionModel functionModel = new FunctionModel();
            functionModel.setImageResource(functionIconList[i]);
            functionModel.setTitle(functionTitleList[i]);
            list.add(functionModel);
        }
//        fragment_home_page_rec_function.setLayoutManager(new LinearLayoutManager(mBaseActivity, LinearLayoutManager.HORIZONTAL, false));
        fragment_home_page_rec_function.setLayoutManager(new GridLayoutManager(mBaseActivity, 3));
        HomePageFunctionAdapter homePageFunctionAdapter = new HomePageFunctionAdapter(R.layout.fragment_home_page_item_function, list);
        homePageFunctionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (position) {
                    case 0:
                     startOtherActivity(DeviceActivity.class,null);
                        break;
                    case 1:
                        startOtherActivity(PermissionManageActivity.class,null);
                        break;
                    case 2:
                        startOtherActivity(MainActivity2.class,null);
                        break;
                    case 3:
                        break;
                    default:
                        break;
                }
            }
        });
        fragment_home_page_rec_function.setAdapter(homePageFunctionAdapter);
    }
    protected void setStatusBar() {
        //设置状态栏颜色和位移一段距离
        DisplayHelper.setStatusBarColor(mBaseActivity, R.color.common_head);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) fragment_home_page_rec_function.getLayoutParams();
        lp.setMargins(0, DisplayHelper.getStatusBarHeight(mBaseActivity), 0, 0);
    }
}
